package com.zzyl.controller;


import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * 角色前端控制器
 */
@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /***
     *  多条件查询角色分页列表
     * @param roleDto 角色Vo查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return: PageResponse<roleVo>
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页",notes = "角色分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto"),
        @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
        @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    public ResponseResult<PageResponse<RoleVo>> findRoleVoPageResponse(
                                    @RequestBody RoleDto roleDto,
                                    @PathVariable("pageNum") int pageNum,
                                    @PathVariable("pageSize") int pageSize) {
        PageResponse<RoleVo> pageResponse = roleService.findRolePage(roleDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     *  保存角色
     * @param roleDto 角色DTO对象
     * @return RoleDto
     */
    @PutMapping
    @ApiOperation(value = "角色添加",notes = "角色添加")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "RoleDto")
    public ResponseResult<RoleVo> createRole(@RequestBody RoleDto roleDto) {
        Boolean flag = roleService.createRole(roleDto);
        return ResponseResult.success(flag);
    }


    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation(value = "根据角色查询选中的资源数据")
    @ApiImplicitParam(name = "roleId",value = "角色ID",required = true,dataType = "Long")
    public ResponseResult<Set<String>> findCheckedResources(@PathVariable Long roleId) {
        log.info("根据角色查询选中的资源数据: {}", roleId);
        Set<String> numbering = roleService.findCheckedResources(roleId);
        return ResponseResult.success(numbering);
    }
    @DeleteMapping("/{roleIds}")
    @ApiOperation(value = "根据id删除角色和关联映射的资源")
    @ApiImplicitParam(name = "roleIds",value = "角色ID",required = true,dataType = "List<String>")
    public ResponseResult deleteRole(@PathVariable List<String> roleIds) {
        log.info("根据角色查询选中的资源数据: {}", roleIds);
         roleService.deleteRole(roleIds);
        return ResponseResult.success();
    }
    @PatchMapping
    @ApiOperation(value = "角色修改")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "RoleDto")
    public ResponseResult<Boolean> updateRole(@RequestBody RoleDto roleDto) {
        log.info("根据id修改数据: {},id :{}", roleDto,roleDto.getId());
       Boolean flag = roleService.updateRole(roleDto);
        return ResponseResult.success(flag);
    }

    @PostMapping("/init-roles")
    @ApiOperation(value = "角色下拉框")
    public ResponseResult<List<RoleVo>> initRole() {
        log.info("根据id修改数据:");
        List<RoleVo> roleVos = roleService.initRole();
        return ResponseResult.success(roleVos);
    }

}
